#include "GUI_Morph.h"

//===Mutators
void gui_morph::Init(point Pos, int ID)
{
	m_location = Pos;
	AssetIndex = ID;
}
void gui_morph::InitAsMenu(int ID, bool(*check)(),Quad Bound)
{
	Init(point(Bound.x,Bound.y),ID);
	m_qBound			= Bound;
	Morph				= MORPH_PANEL;
	ShowFunction		= check;	
	m_bActive			= false;
	ExecuteFunct		= NULL;
	m_tbTexts			= NULL;
	m_iElements			= 0;
	m_iAllocatedSpace	= 0;
};
void gui_morph::InitAsButton(int ID, bool(*check)(),Quad Bound,void (*execute)())
{
	Init(point(Bound.x,Bound.y),ID);
	m_qBound			= Bound;
	Morph				= MORPH_BUTTON;
	ShowFunction		= check;	
	m_bActive			= false;
	ExecuteFunct		= execute;
	m_tbTexts			= NULL;
	m_iElements			= 0;
	m_iAllocatedSpace	= 0;
};
void gui_morph::addTextBox(Quad Pos, char* Value)
{
	if(Morph != MORPH_NULL)
	{
		TextBox ToAdd(Value,Pos);
		if(m_iAllocatedSpace == 0)
		{
			m_iAllocatedSpace	= oneLength;
			m_tbTexts			= new TextBox[m_iAllocatedSpace];
		}

		if(m_iElements == m_iAllocatedSpace)
		{
			m_iAllocatedSpace += oneLength;
			TextBox* Temp = new TextBox[m_iAllocatedSpace];
			for(int i(0); i < m_iElements; i++)	
				Temp[i] = m_tbTexts[i];
			delete m_tbTexts;				//Delete the old pointer	
			m_tbTexts = Temp;				//Point at the new

		}

		m_tbTexts[m_iElements] = ToAdd;
		m_iElements++;
	}
};
void gui_morph::update()
{
	//WARNING: Don't call update unless you need to, NOT every cycle!
	//May become too intensive otherwise
	if(Morph != MORPH_NULL)
	{
		if(ShowFunction() == 0)
			m_bActive = false;
		else	
		m_bActive = true;
	}
}


bool gui_morph::checkCollision(point Loc){
	int X((int)Loc.x),Y((int)Loc.y);
	if(X >= m_qBound.x && X <= m_qBound.x+m_qBound.width)
		if(Y >= m_qBound.y && Y <= m_qBound.y+m_qBound.height)
		{
			ExecuteFunct();
			return true;
		}
		return false;
};



//Accessors 
TextBox gui_morph::access(int Index)
{
	return m_tbTexts[Index];
};
